package com.hp.test.concurrent.producer_consumer.pc2;

import lombok.Getter;
import lombok.Setter;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 用await和signal实现生产者消费者
 *
 * @author hupan
 * @date 2021-05-03 14:10
 */
public class ProducerAndConsumer2 {

    public static void main(String[] args) {
        Storage storage = new Storage();
        Producer producer = new Producer("生产者", storage);
        Consumer consumer = new Consumer("消费者", storage);

        new Thread(producer).start();
        new Thread(consumer).start();
    }

}


class Storage {
    // 仓库最大存储量
    @Getter
    private final int MAX_SIZE = 10;
    // 锁
    private final Lock lock = new ReentrantLock();
    // 仓库满的条件变量
    private final Condition full = lock.newCondition();
    // 仓库空的条件变量
    private final Condition empty = lock.newCondition();
    // 仓库存储的载体
    @Getter
    @Setter
    private LinkedList<Object> list = new LinkedList<>();

    // 生产产品
    public void produce(String producer) {
        lock.lock();

        try {
            // 如果仓库已满
            while (list.size() == MAX_SIZE) {
                System.out.println("仓库已满，【" + producer + "】： 暂时不能执行生产任务!");
                try {
                    // 由于条件不满足，生产阻塞
                    full.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            // 生产产品
            list.add(new Object());
            System.out.println("【" + producer + "】：生产了一个产品\t【现仓储量为】:" + list.size());
            empty.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            lock.unlock();
        }

    }

    // 消费产品
    public void consume(String consumer) {
        // 获得锁
        lock.lock();

        try {
            // 如果仓库存储量不足
            while (list.size() == 0) {
                System.out.println("仓库已空，【" + consumer + "】： 暂时不能执行消费任务!");
                try {
                    // 由于条件不满足，消费阻塞
                    empty.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            list.remove();
            System.out.println("【" + consumer + "】：消费了一个产品\t【现仓储量为】:" + list.size());
            full.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            lock.unlock();
        }

    }
}



@Getter
@Setter
class Producer extends Thread {
    private String producer;
    private Storage storage;

    public Producer(String producer, Storage storage) {
        this.producer = producer;
        this.storage = storage;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            storage.produce(producer);
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}


@Getter
@Setter
class Consumer extends Thread {
    private String consumer;
    private Storage storage;

    public Consumer(String consumer, Storage storage) {
        this.consumer = consumer;
        this.storage = storage;
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            storage.consume(consumer);
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}




